package com.itheima.service.impl.system;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.system.SysLog;
import com.itheima.mapper.system.SysLogMapper;
import com.itheima.service.system.SysLogService;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.SimpleFormatter;

public class SysLogServiceImpl implements SysLogService {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public List<SysLog> findAll(Integer pageNum, Integer pageSize) {
        SysLogMapper mapper = MapperUtil.getMapper(SysLogMapper.class);
        PageHelper.startPage(pageNum, pageSize);
        List<SysLog> list = mapper.findAll();
        for (SysLog sysLog : list) {
            sysLog.setVisitTimeStr(sdf.format(sysLog.getVisitTime()));
        }
        return list;
    }


    @Override
    public void save(SysLog sysLog) {
        SysLogMapper mapper = MapperUtil.getMapper(SysLogMapper.class);

        mapper.save(sysLog);
    }


    @Override
    public ByteArrayOutputStream getReport() throws IOException {
        XSSFWorkbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("test");
        //合并单元格
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 7));
        //规定单元格样式
        XSSFCellStyle cellStyle = wb.createCellStyle();
        //设置单元格居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        Row row_1 = sheet.createRow(1);
        Cell cell_1_1 = row_1.createCell(1);
        cell_1_1.setCellStyle(cellStyle);
        //表头
        cell_1_1.setCellValue("题目数据文件");
        //标题
        String[] fields = {"ID", "访问时间", "访问用户", "访问IP", "资源URL",
                "执行时间", "访问方法"};
        Row row_2 = sheet.createRow(2);
        for (int i = 0; i < fields.length; i++) {
            Cell cell_temp = row_2.createCell(1 + i);
            cell_temp.setCellValue(fields[i]);
            cell_temp.setCellStyle(cellStyle);
        }

        //制作数据区
        SysLogMapper mapper = MapperUtil.getMapper(SysLogMapper.class);
        List<SysLog> sysLogList = mapper.findAll();

        int row_index = 0;
        for (SysLog sysLog : sysLogList) {
            int cell_index = 0;
            Row row_temp = sheet.createRow(3 + row_index++);
            Cell cell_data_0 = row_temp.createCell(1 + cell_index++);
            cell_data_0.setCellValue(sysLog.getId());
            Cell cell_data_1 = row_temp.createCell(1 + cell_index++);
            String dateStr = sdf.format(sysLog.getVisitTime());
            cell_data_1.setCellValue(dateStr);
            Cell cell_data_2 = row_temp.createCell(1 + cell_index++);
            cell_data_2.setCellValue(sysLog.getUsername());
            Cell cell_data_3 = row_temp.createCell(1 + cell_index++);
            cell_data_3.setCellValue(sysLog.getIp());
            Cell cell_data_4 = row_temp.createCell(1 + cell_index++);
            cell_data_4.setCellValue(sysLog.getUrl());
            Cell cell_data_5 = row_temp.createCell(1 + cell_index++);
            cell_data_5.setCellValue(sysLog.getExecutionTime());
            Cell cell_data_6 = row_temp.createCell(1 + cell_index++);
            cell_data_6.setCellValue(sysLog.getMethod());

            cell_data_0.setCellStyle(cellStyle);
            cell_data_1.setCellStyle(cellStyle);
            cell_data_2.setCellStyle(cellStyle);
            cell_data_3.setCellStyle(cellStyle);
            cell_data_4.setCellStyle(cellStyle);
            cell_data_5.setCellStyle(cellStyle);
            cell_data_6.setCellStyle(cellStyle);

        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        wb.close();
        return os;
    }


    @Override
    public void delete(String ids) {
        SqlSession session = MapperUtil.getSession();
        SysLogMapper mapper = session.getMapper(SysLogMapper.class);
        String[] idStr = ids.split(",");
        for (String s : idStr) {
            mapper.delete(s);
            session.commit();
        }
        session.close();

    }

    @Override
    public List<SysLog> findByUsername(String username,int pageN,int pageS) {
        SysLogMapper mapper = MapperUtil.getMapper(SysLogMapper.class);

        PageHelper.startPage(pageN,pageS);
        List<SysLog> list = mapper.findByUsername(username);

        for(SysLog sysLog :list) {
            sysLog.setVisitTimeStr(sdf.format(sysLog.getVisitTime()));
        }
        return list;
    }







}
